package com.grapes.view;

import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.grapes.R;
import com.grapes.business.CompanyRelativeBusiness;
import com.grapes.model.CampusTalking;
import com.grapes.model.NetApplication;

public class CompanyCampusTalkingActivity extends GrapesActivity {
	private List<CampusTalking> campusTalkings;
	private String companyId;
	private String companyName;
	private NetApplication netApplication;
	private int selectedCampusTalkingIndex;
	public static final int MSG_LOAD_NET_APPLICATION_OK = 1;
	public static final int MSG_LOAD_NET_APPLICATION_FAIL = 2;
	public static final int MSG_LOAD_NET_CAMPUS_TALKING_OK = 3;
	public static final int MSG_LOAD_NET_CAMPUS_TALKING_FAIL = 4;

	public Handler hander = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch (msg.what) {
			case MSG_LOAD_NET_APPLICATION_OK:
				loadCampusTalkings();
				break;
			case MSG_LOAD_NET_APPLICATION_FAIL:
				RelativeLayout net_application_group = (RelativeLayout) findViewById(R.id.company_campus_talking_net_application_group);
				net_application_group.setVisibility(View.GONE);
				loadCampusTalkings();
				break;
			case MSG_LOAD_NET_CAMPUS_TALKING_OK:
			case MSG_LOAD_NET_CAMPUS_TALKING_FAIL:
				dismissPorgressDialog();
				ListView campusTalkingListView = (ListView) findViewById(R.id.company_campus_talking_list);
				CompanyCampusTalkingAdapter adapter=new CompanyCampusTalkingAdapter();
				campusTalkingListView.setAdapter(adapter);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.company_campus_talkings);
		Button backBtn = (Button) findViewById(R.id.company_campus_talking_title_back_btn);
		backBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				goBack();
			}
		});

		companyName = getIntent().getStringExtra(
				CompanyFavoritorActivity.INTENT_EXTRA_COMPANY_NAME);
		TextView titleView = (TextView) findViewById(R.id.company_campus_talking_title_text);
		titleView.setText(companyName);

		companyId = getIntent().getStringExtra(
				CompanyFavoritorActivity.INTENT_EXTRA_COMPANY_ID);

		Button storeNetApplictionBtn = (Button) findViewById(R.id.company_campus_talking_net_application_store_btn);
		storeNetApplictionBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				storeNetAppliction();
			}
		});

		Button shareNetApplicationBtn = (Button) findViewById(R.id.company_campus_talking_net_application_share_btn);
		shareNetApplicationBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				shareNetApplication();
			}
		});

		showProgressDialog();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					netApplication = CompanyRelativeBusiness.getInstance()
							.getFakedCompanyNetApplication(companyId);
					Message msg = new Message();
					msg.what = MSG_LOAD_NET_APPLICATION_OK;
					hander.sendMessage(msg);
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = MSG_LOAD_NET_APPLICATION_FAIL;
					hander.sendMessage(msg);
				}
			}
		}).start();
	}

	private void loadCampusTalkings() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					campusTalkings = CompanyRelativeBusiness.getInstance()
							.getFakedCompanyCampusTalking(companyId);
					Message msg = new Message();
					msg.what = MSG_LOAD_NET_CAMPUS_TALKING_OK;
					hander.sendMessage(msg);
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = MSG_LOAD_NET_CAMPUS_TALKING_FAIL;
					hander.sendMessage(msg);
				}
			}
		}).start();
	}

	private void storeNetAppliction() {

	}

	private void shareNetApplication() {

	}

	private void shareSelectedCampusTalking() {

	}

	private void storeSelectedCampusTalking() {

	}

	private class CellViewHolder {
		TextView schoolNameTxt;
		TextView campusTalkingAddressTxt;
		TextView startDateTimeTxt;
		Button shareBtn;
		Button storeBtn;
	}

	class CompanyCampusTalkingAdapter extends BaseAdapter {
		CellViewHolder viewHolder;
		TextView schoolNameTxt;
		TextView campusTalkingAddressTxt;
		TextView startDateTimeTxt;
		Button shareBtn;
		Button storeBtn;

		@Override
		public int getCount() {
			return campusTalkings.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final int index=position;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.company_campus_talking_item, null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.campusTalkingAddressTxt = (TextView) convertView
						.findViewById(R.id.company_campus_talking_address);
				viewHolder.startDateTimeTxt = (TextView) convertView
						.findViewById(R.id.company_campus_talking_date_time);
				viewHolder.schoolNameTxt = (TextView) convertView
						.findViewById(R.id.company_campus_talking_school_name);

				// Button
				viewHolder.shareBtn = (Button) convertView
						.findViewById(R.id.company_campus_talking_share_btn);
				viewHolder.storeBtn = (Button) convertView
						.findViewById(R.id.company_campus_talking_store_btn);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			startDateTimeTxt = viewHolder.startDateTimeTxt;
			campusTalkingAddressTxt = viewHolder.campusTalkingAddressTxt;
			schoolNameTxt = viewHolder.schoolNameTxt;
			shareBtn = viewHolder.shareBtn;
			shareBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					selectedCampusTalkingIndex=index;
					shareSelectedCampusTalking();
				}
			});
			
			storeBtn = viewHolder.storeBtn;
			storeBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					selectedCampusTalkingIndex=index;
					storeSelectedCampusTalking();					
				}
			});
			
			CampusTalking campusTalking=campusTalkings.get(index);
			startDateTimeTxt.setText(campusTalking.getDate()+" "+campusTalking.getTime());
			campusTalkingAddressTxt.setText(campusTalking.getAddress());
			schoolNameTxt.setText(campusTalking.getSchoolName());
			return convertView;
		}
	}

}
